﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using NhbHelper;
using NhbHelper.Entities;
using NHibernate;
using NhbHelper.DataAccess;

using FileTransferServiceReference;


public partial class _Default : System.Web.UI.Page
{

    private static List<SavedReconciliation> SavedRecs;
    protected ISession m_session;
    protected int ReportTable;
   
    
    protected void Page_Load(object sender, EventArgs e)
    {                      
        
        if (!Page.IsPostBack)
        {           
            ReloadLoadRecs();
            MultiView1.SetActiveView(DefaultView);                        
        }
        DisplayRecs();
    }

    override protected void OnInit(EventArgs e)
    {
        // move your initializers here
        UploadSourceFile2.SaveCompleted += new EventHandler(SaveRecAction_Completed);

    }

    private void SaveRecAction_Completed(object sender, EventArgs e)
    {
        pnlSavedRecs.Controls.Clear();
        ReloadLoadRecs();
        DisplayRecs();
        LinkButton_Command(sender, new CommandEventArgs("DefaultView","DefaultView"));
        //redisplay default main view.

    }
    private void DisplayRecs()
    {
        
        foreach (SavedReconciliation sr in SavedRecs)
        {
            ASP.RecItem RecItemX = (ASP.RecItem)LoadControl("~/WebControls/RecItem.ascx");;
            RecItemX.Rec = sr;
            RecItemX.Evt_Table_Click += new EventHandler(Evt_OpenTable);       
            pnlSavedRecs.Controls.Add(RecItemX);
        }
    }

    void Evt_OpenTable(object sender, EventArgs e)
    {        
        LinkButton_Command(sender, new CommandEventArgs(((MyEventArgs)e).TableName,"ReportView"));
        
    }


    private void ReloadLoadRecs()
    {
        SavedRecs = new List<SavedReconciliation>(); //create new list of SavedRecs
        m_session = NHibernateHttpModule.CurrentSession;
        IList<Reconciliation> myList = m_session.GetNamedQuery("GetAllReconciliations").SetParameter("rec_name", "TestRec").List<Reconciliation>();

        foreach (Reconciliation rec in myList)
        { 
           SavedReconciliation x  = new SavedReconciliation();
            x.ID = rec.Rec_Id;
            x.LastRun = rec.Last_Run;
            x.LeftTable = rec.Tbl_Left_Name;
            x.RightTable = rec.Tbl_Right_Name;
            x.Name = rec.Rec_Name;
            x.Owner = rec.Owner;
            x.OutPutTable = rec.Tbl_Output_Name;            
            SavedRecs.Add(x);           
        }
        
    }
     

    protected void LinkButton_Command(object sender, CommandEventArgs e)
    {
        switch (e.CommandArgument.ToString())
        {
            case "DefaultView":
                MultiView1.SetActiveView(DefaultView);
                break;
            case "NewRecView":
                {
                    m_session = NHibernateHttpModule.CurrentSession;
                    ISQLQuery iq = m_session.CreateSQLQuery("select seq_rec_num.nextval as Identifier from dual");
                    iq.AddScalar("Identifier", NHibernateUtil.String);
                    UploadSourceFile2.GetUniqueRecID = Convert.ToInt32((iq.List())[0]);
                    UploadSourceFile2.RefreshRecNumLabel();
                    MultiView1.SetActiveView(NewRecView);
                    break;
                }
            case "ReportView":
                {
                    //m_session = NHibernateHttpModule.CurrentSession;
                    //ISQLQuery iq = m_session.CreateSQLQuery("select * from " + e.CommandName); //abit muddled..
                    gvTableOutput.DataSource = DBObject.RetrieveAllRecs("select * from " + e.CommandName);
                    gvTableOutput.DataBind();
                    MultiView1.SetActiveView(ReportView);
                    break;
                }
            default:
                throw new Exception("You didn't select a valid list item.");
                
        }
    }


}
